var dataUrl = null;
var pageii = null;
$(function () {
    init();
    $("#generateReport").click(function () {
        generateReport();
    });

});

function init() {
    // dataUrl = $("#dataUrl").val();
    dataUrl = "/modules";
    getCorpName();
    // getReportCategory();
    // findReportVersion();
}
function deleteVersion(id) {
    var url =  "/reportManage/deleteReportVersion?id="+id+"&newDate=" + (new Date().getTime());
    $.ajax({
        url: url,
        method: "get",
        dataType: "text",
        success: function (result) {
            if (result != null) {
                layer.msg("删除成功")
                findReportVersion()
            } else {
                layer.msg("删除失败！");

            }
        }
    });

}
/**
 * 获取单位名称
 * @param cropId
 */
function getCorpName() {
    var url = dataUrl + "/basic//corp/all" + "?newDate=" + (new Date().getTime());
    ;
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#corpName").html(html);
                $('#corpName').searchableSelect({
                    afterSelectItem: function () {
                        getCorpPostList(this.holder.data("value"))
                    }
                });
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

/**
 * 获取单位岗位名称
 * @param cropId
 */
function getCorpPostList(corpId) {
    var url = dataUrl + "/homepage/getCorpPostByCorpIds?corpId=" + corpId + "&newDate=" + (new Date().getTime());
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#corpProp").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });


    // findReportVersion();
}


function findReportVersion2() {
    var url = dataUrl + "/statisticsReport/statisticsDetails?newDate=" + (new Date().getTime());
    // $("#propName").find("option:selected").attr("data-id");
    var corpId = $("#corpName").val();
    if (corpId != null && corpId != "" && corpId != undefined) {
        url += "&&corpId=" + corpId;
    }
    var corpPropId = $("#corpProp").val();
    if (corpPropId != null && corpPropId != "" && corpPropId != undefined) {
        url += "&&postId=" + corpPropId;
    } else {
        return;
    }
    /*  var reportType = $("#reportType").val();
      if (reportType != null && reportType != "" && reportType != undefined) {
          url += "&&subType=" + reportType;
      }*/
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            debugger
            var html = "";
            if (result != null) {
                if (result.length > 0) {
                    // $("#dataTable").show();
                    // $("#generateReport").hide();
                    for (var i = 0; i < result.length; i++) {
                        debugger
                        var rowspan = 1;

                        if (result[i].inspectItemList != undefined && result[i].inspectItemList != null && result[i].inspectItemList != "" && result[i].inspectItemList.length > 0) {
                            rowspan = result[i].inspectItemList.length
                            html += "  <tr style='background: #fff'>" ;
                            html += "  <th rowspan='" + rowspan + "'>" + result[i].projectName + "</th> ";
                            for (var j = 0; j < result[i].inspectItemList.length; j++) {
                                html += "                <th >" +result[i].inspectItemList[j].checkItem_Demand+ "</th> " +
                                    "                <th >" +result[i].inspectItemList[j].Weighting+ "</th> " +

                                    "                <th >" ;
                                for(var y = 0; y < result[i].inspectItemList[j].checkCountList.length; y++){
                                    html +=result[i].inspectItemList[j].checkCountList[y].corpCaption+"-<br/>"
                                    html +=result[i].inspectItemList[j].checkCountList[y].name+"<br/>"
                                }
                                html +=     "</th> " +
                                    "                <th >"+result[i].inspectItemList[j].scoring+"</th> " +
                                    "                <th >" +result[i].inspectItemList[j].Expression_Desc+ "</th> " +
                                    "                <th >" +crtTimeFtt(result[i].inspectItemList[j].Check_time)+ "</th>" ;
                            if(j < result[i].inspectItemList.length-1){
                                html +="</tr><tr style='background: #fff'>"
                            }
                            }
                            html += "            </tr>";
                        }else {
                            html += "  <tr>" ;
                            html += " <th>" + result[i].projectName + "</th> ";
                            html += "                <th >" + "</th> " +
                                "                <th ></th> " +
                                "                <th ></th> " +
                                "                <th ></th> " +
                                "                <th ></th> " +
                                "                <th ></th> ";
                            html += "            </tr>";
                        }

                    }
                } /*else {
                    $("#dataTable").hide();
                    // $("#generateReport").show();
                }*/

                $("#reportVersion").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");

            }
        }
    });
}

function findReportVersion() {
    var url = dataUrl + "/basicReport/findReportVersion?type=3&subType=1&newDate=" + (new Date().getTime());
    // $("#propName").find("option:selected").attr("data-id");
    var corpId = $("#corpName").val();
    if (corpId != null && corpId != "" && corpId != undefined) {
        url += "&&corpId=" + corpId;
    }
    var postId = $("#corpProp").val();
    if (postId != null && postId != "" && postId != undefined) {
        url += "&&postId=" + postId;
    }else {
        return;
    }
  /*  var reportType = $("#reportType").val();
    if (reportType != null && reportType != "" && reportType != undefined) {
        url += "&&subType=" + reportType;
    }*/
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {debugger
            var html = "";
            if (result != null) {
                $("#dataTable").show();
                if(result.length>0){
                    // $("#generateReport").hide();
                    for (var i = 0; i < result.length; i++) {
                        html += "  <tr role=\"row\" class=\"odd\">" +
                            "  <th width=\"75px\"><a onclick=\"reportLook('" + result[i].url + "')\">" + result[i].caption + "</a>\n" +
                            "            </th>" +
                            "            <th width=\"150px\">" + crtTimeFtt(result[i].time) + "</th>" +
                            "            <th width=\"150px\">" +
                            "<a class='dropdown-toggle' style='color: #197eb8'  href='javascript:download(\""+result[i].url+"\")'>下载</a>&nbsp;&nbsp;&nbsp;"+
                            "<a class='dropdown-toggle' style='color: red'  href='javascript:deleteVersion(\""+result[i].id+"\")'>删除</a>" +
                             "</th>" +
                            "</tr>";
                    }
                }else{
                    // $("#dataTable").hide();
                    // $("#generateReport").show();
                }

                $("#reportVersion").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");

            }
        }
    });
}
/**
 * 生成报表
 */
function generateReport() {
    var url = dataUrl + "/basicReport/generateReport?type=3&subType=1&newDate=" + (new Date().getTime());
    var projectId = $("#projectName").val();
    var corpId = $("#corpName").val();
    if (corpId != null && corpId != "" && corpId != undefined) {
        url += "&&corpId=" + corpId;
    }
    var postId = $("#corpProp").val();
    if (postId != null && postId != "" && postId != undefined) {
        url += "&&postId=" + postId;
    }else {
        return;
    }
    $.ajax({
        url: url,
        method: "get",
        dataType: "text",
        success: function (result) {
            if(result=='success'){
                layer.msg("生成成功");
                findReportVersion();
            }else{
                findReportVersion();
                // layer.msg("生成失败");
            }
        }
    });
}
/**
 * 预览
 * @param url
 */
function reportLook(url) {
    window.open("https://view.officeapps.live.com/op/view.aspx?src=" + url);
}

/**
 * 下载
 * @param url
 */
function download(url) {
    window.open( url);
}




//创建时间格式化显示
function crtTimeFtt(value, row, index) {
    var crtTime = new Date(value);
    return dateFtt("yyyy-MM-dd hh:mm:ss", crtTime);//直接调用公共JS里面的时间类处理的办法
}

/**************************************时间格式化处理************************************/
function dateFtt(fmt, date) { //author: meizz
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
